#include "window7.h"
#include <QPalette>
#include <QColor>
#include <QDebug>
#include <QKeyEvent>
#include <QMenuBar>
#include <QMenu>
#include <QPushButton>
#include <QMessageBox>
#include <QIcon>
#include <QBitmap>
#include "globalutils.h"
#include <QCoreApplication>
Window7::Window7(QWidget *parent) : QMainWindow(parent)
{
    //初始化窗口
    initWin();
    //someTest();
}
/**
 * 初始化窗口设置
 * 如大小,标题...
 * @brief Window7::initWin
 */
void Window7::initWin(){
    //设置无边框
    // this->setWindowFlags(Qt::FramelessWindowHint);
    move(400, 200);
    //固定窗口大小
    setFixedSize(SNACK_WIN_WIDTH, SNACK_WIN_HEIGHT);
    //设置背景色
    QPalette palette = this->palette();
    palette.setColor(QPalette::ColorRole::Background, QColor(0x26, 0x32,0x38));
    this->setPalette(palette);
    //设置图标
    QIcon icon(QBitmap(QCoreApplication::applicationDirPath().append("/resources/snake.png")));
    this->setWindowIcon(icon);
    setWindowTitle(tr("贪吃蛇游戏"));

    //初始化组件
    initMenu();
    initSnakeWidget();
    initBoard();


}
/**
 * 添加游戏窗口
 * @brief Widget::showNewWidget
 */
void Window7::initSnakeWidget(){

   snakeWidget = new  SnakeWidget(this);
   snakeWidget->setFixedSize(GAME_AREA_WIDTH, GAME_AREA_HEIGHT); //固定大小
   snakeWidget->move(10, menuBarHeiht); //设置位置

   QObject::connect(snakeWidget, SIGNAL(signalGameStart(int , int )),
           this, SLOT(slotGameStart(int , int )));
   QObject::connect(snakeWidget, SIGNAL(signalGetScore(int , int )),
           this, SLOT(slotUpdateScore(int , int )));


   //设置背景色（子窗口继承父窗口的调色板 设置了也不起作用）
//   QPalette palette = SnakeWidget->palette();
//   palette.setColor(QPalette::Background, QColor(00, 00,255));
//   SnakeWidget->setPalette(palette);
}

/**
 * 添加看板
 * @brief Window7::initBoard
 */
void Window7::initBoard(){
    boardWidget = new SnakeBoardWidget(this);
    boardWidget->setFixedSize( width() - GAME_AREA_WIDTH - 30, GAME_AREA_HEIGHT); //固定大小
    boardWidget->move(10 + GAME_AREA_WIDTH + 10, menuBarHeiht); //设置位置
}
/**
 * 键盘事件
 * @brief Window7::keyPressEvent
 * @param event
 */
void Window7::keyPressEvent(QKeyEvent *event){
    //qDebug() << "主窗口：" << event->key();
    if(snakeWidget != nullptr){
        //将事件传递给游戏窗口
        snakeWidget->keyPressEventFromWindow(event);
    }
}

/**
 * 开始游戏
 * @brief Window7::startGame
 */
void Window7::startGame(){
    snakeWidget->startGame();
}
/**
 * 接收信息
 * @brief slotGameStart
 * @param currentLevel 当前关卡
 * @param passRequiredScore 通关分数
 */
void Window7::slotGameStart(int currentLevel, int passRequiredScore){
    boardWidget->setLevel(currentLevel);
    boardWidget->setPassScore(passRequiredScore);
}
/**
 * 得分信息
 * @brief slotUpdateScore
 * @param totalScore 总分
 * @param currentLevelScore 当前关卡得分
 */
void Window7::slotUpdateScore(int totalScore, int currentLevelScore ){
    boardWidget->setTotalScore(totalScore);
    boardWidget->setCurrentScore(currentLevelScore);
}
/**
 * 初始化菜单
 * @brief Window7::initMenu
 */
void Window7::initMenu(){
    menuBarHeiht = menuBar()->height();
    gameMenu = menuBar()->addMenu(tr( "游戏" ));
    startAction = new QAction(this);
    startAction->setText(tr("开始游戏"));
    startAction->setShortcut(tr("Ctrl+N" ));
    gameMenu->addAction(startAction);
    connect(startAction, SIGNAL(triggered()), this, SLOT(startGame()));
}

void Window7::someTest(){
   int num =  GlobalUtils::getRandomNum(123,10);
   qDebug() << "随机数:" << num << "\n";
}
Window7::~Window7(){}
